<include file="Public/header"/>
<include file="Public/menu"/>

<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            商品
            <small>管理</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('goodsCat/index')}"><i class="fa fa-dashboard"></i> 商品</a></li>
            <li class="active">添加商品分类</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">修改分类</h3>

                        <div class="box-tools">

                            <div class="btn-group pull-right" style="margin-right: 10px">
                                <a href="{:U('goodsCat/index')}" class="btn btn-sm btn-default"><i class="fa fa-list"></i>&nbsp;分类列表</a>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form action="{:U('goodsCat/update')}" method="post" id="form" class="form-horizontal">
                        <input type="hidden" name="cat_id" value="{$cat['cat_id']}">
                        <div class="box-body">
                            <div class="form-group 1">
                                <label class="col-sm-2 control-label">分类级别</label>
                                <div class="col-sm-6">
                                    <select class="form-control  select2-hidden-accessible level" style="width: 100%;" name="level">
                                        <option value="1">一级分类</option>
                                        <option value="2">二级分类</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group 1">
                                <label class="col-sm-2 control-label">导航菜单</label>
                                <div class="col-sm-6">
                                    <select class="form-control  select2-hidden-accessible parent_id" style="width: 100%;" name="parent_id" disabled="disabled">
                                        <option value="">请选择</option>
                                        <?php foreach($parent_list as $v) {
                                            if ($v['cat_id'] != $cat['cat_id']) {
                                        ?>
                                        <option value="<?php echo $v['cat_id']; ?>" <?php echo $v['cat_id'] == $cat['parent_id'] ? 'selected':'';?>><?php echo $v['cat_name']; ?></option>

                                        <?php } } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group 1">
                                <label class="col-sm-2 control-label">分类名称</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-pencil"></i></span>
                                        <input type="test" name="cat_name" value="{$cat['cat_name']}" class="form-control" placeholder="分类名称">
                                        <input type="hidden" name="id" value="{$cat['store_cat_id']}">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <div class="col-sm-2">

                            </div>
                            <div class="col-sm-6">

                                <div class="btn-group pull-left">
                                    <button id='hahah' type="submit" class="btn btn-info pull-right">提交</button>
                                </div>


                            </div>

                        </div>
                        <!-- /.box-footer -->
                    </form>
                </div>

            </div>
        </div>

    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>
<!--上传插件-->
<link rel="stylesheet" href="/Public/admin/fileinput/fileinput.min.css">
<script src="/Public/admin/fileinput/fileinput.min.js"></script>
<script data-exec-on-popstate>

    $(function () {

        $('.level').change(function () {
            var num = $(this).val();
            if (num == 2) {
                $('.parent_id').removeAttr('disabled');
                $('.suggest-1').hide();
                $('.suggest-2').show();
            }else {
                $('.parent_id').attr('disabled','disabled');
                $('.suggest-2').hide();
                $('.suggest-1').show();
            }
        });


        // 上传图片js
        $("#image").fileinput({
            overwriteInitial: true,
            dropZoneEnabled: false,
            uploadUrl: '/admin/upload/index/',
            language: 'zh',
            showRemove: false,
            maxFileCount: 1,
            browseClass: 'btn btn-primary btn-docup',
            showPreview: true,
            uploadClass: 'btn btn-default btn-docup',
            allowedFileTypes: ["image"],
            browseLabel: "添加图片",
            uploadLabel: "上传图片",
            initialCaption:"{$cat['cat_img']}",
            <if condition = "(!empty($cat['cat_img']))">
            initialPreview:['<img src="/Uploads/{$cat[cat_img]}" class="file-preview-image">']
            </if>
        }).on("fileuploaded", function (event, data, previewId, index) {
            var response = data.response;

            if (data.jqXHR.status == 200) {
                // ajax 成功返回数据写入
                $('.file-preview-frame').eq(index).append("<input class='abc' type='hidden' name='cat_img[]' value='"+response+"'>");
            }
            if (response.info != undefined ){
                alert(response.info);
            }

        });
        $("#image").on('filecleared', function(event) {
            $("#image_action").val(1);
        });


        $("#cat_bg_img").fileinput({
            overwriteInitial: true,
            dropZoneEnabled: false,
            uploadUrl: '/admin/upload/index/"',
            language: 'zh',
            showRemove: false,
            maxFileCount: 1,
            browseClass: 'btn btn-primary btn-docup',
            showPreview: true,
            uploadClass: 'btn btn-default btn-docup',
            allowedFileTypes: ["image"],
            browseLabel: "添加图片",
            uploadLabel: "上传图片",
/*            initialCaption:"{$cat['cat_bg_img']}",
            <if condition= "(!empty($cat['cat_bg_img']))">
            initialPreview:['<img src="/Uploads/{$cat[cat_bg_img]}" class="file-preview-image">' + '<div class="file-thumbnail-footer"><div class="file-footer-caption" title="{$cat[cat_bg_img]}">{$cat[cat_bg_img]}</div><div class="file-thumb-progress"><div class="progress"><div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width:100%;">100%</div></div></div> <div class="file-actions"><div class="file-footer-buttons"><button type="button" class="kv-file-upload btn btn-xs btn-default" title="Upload file" style="display: none;">   <i class="glyphicon glyphicon-upload text-info"></i></button><button type="button" class="kv-file-remove btn btn-xs btn-default" title="Remove file"><i class="glyphicon glyphicon-trash text-danger"  images="cat_bg_img"></i></button></div><div class="file-upload-indicator" title="Uploaded"><i class="glyphicon glyphicon-ok-sign text-success"></i></div></div></div>']
        </if>*/

        }).on("fileuploaded", function (event, data, previewId, index) {
            var response = data.response;
            if (data.jqXHR.status == 200) {
                // ajax 成功返回数据写入
                $('.file-preview-frame').eq(index).append("<input class='abc' type='hidden' name='cat_bg_img[]' value='"+response+"'>");
            }
        });
        $("#cat_bg_img").on('filecleared', function(event) {
            $("#cat_bg_img_action").val(1);
        });

//        // 删除图片
//        $('.glyphicon').click(function () {
//            var this_img = $('.glyphicon');
//            var type = $(this).attr('images');
//            $.get("/admin/storeCat/deleteImg/id/{$cat['store_cat_id']}/type/" + type,function(data,status){
//                if (data == 1) {
//                    this_img.parents().find('.file-preview').remove();
//                }
//            });
//
//        });

        $('.grid-select-all').change(function() {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });

        $('.batch-delete').on('click', function() {
            var selected = [];
            $('.grid-item:checked').each(function(){
                selected.push($(this).data('id'));
            });

            if (selected.length == 0) {
                return;
            }

            if(confirm("Are you sure to delete this item ?")) {
                $.post('/admin/auth/roles/' + selected.join(), {_method:'delete','_token':'ALpaP7hXZ6s2dnAdE02Z6mUSHPt8RkYbcDRp6rpw'}, function(data){
                    $.pjax.reload('#pjax-container');
                    noty({
                        text: "<strong>Succeeded!</strong>",
                        type:'success',
                        timeout: 1000
                    });
                });
            }
        });
        $("#hahah").submit();

        $(".delete_cat_img").click(function () {
            var type = $(this).attr('images');
            var this_img = $(this);
            $.get("/admin/storeCat/deleteImg/id/{$cat['store_cat_id']}/type/" + type,function(data,status){
                if (data == 1) {
                    this_img.parent().parent().remove();
                }
            });
        });

        //表单验证
        $("#form").validate({
            rules: {
                cat_name:{
                    required: true
                }
            },
            messages:{
                cat_name: {
                    required: "请输入分类名称",
                }
            },
            submitHandler: function(form){
                form.submit();   //提交表单
            }
        });


    });
</script>